import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
import 'package:flutter_app/page_five/DrawerDemo.dart';
import 'page_12/AnimationDemo.dart';
import 'page_eleven/CanvasDemo.dart';
import 'page_eleven/ClipDemo.dart';
import 'page_eleven/DecoratedBoxDemo.dart';
import 'page_eleven/OpacityDemo.dart';
import 'page_eleven/RotatedBoxDemo.dart';
import 'page_five/BottomNavigationDemo.dart';
import 'page_five/CupertinoNavigatorDemo.dart';
import 'page_five/CupertinoStyleDemo.dart';
import 'page_five/PageLayoutDemo.dart';
import 'page_five/PageLayoutDemo2.dart';
import 'page_five/PageLayoutDemo3.dart';
import 'page_five/RouteDemo.dart';
import 'page_five/TextFieldDemo.dart';
import 'page_ten/RouteDemo.dart';
import 'page_ten/RouteDemo2.dart';
import 'pages/demo01.dart';
import 'pages/demo02.dart';
import 'pages/demo03.dart';
import 'pages/demo04.dart';
import 'pages/jsonDemo.dart';
import 'pages/practice02.dart';
import 'pages/practice03.dart';
import 'pages/practice04.dart';
import 'pages/practice05.dart';
import 'pages/practice06.dart';

import 'pages/practice01.dart';
import 'pages/practice07.dart';
import 'weixin_demo/WeixinMain.dart';

void main() => runApp(MyApp());

class ItemBean {
  ItemBean(this.title, this.page);

  String title;
  Widget page;
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    List<ItemBean> data = [];
    data.add(ItemBean('在Flutter中构建布局', Demo01Widget()));
    data.add(ItemBean('使用Material组件', Demo02Widget()));
    data.add(ItemBean('处理手势', Demo03Widget()));
    data.add(ItemBean('表单', Demo04Widget()));
    data.add(ItemBean('Json解析', JsonDemo()));
    data.add(ItemBean("Practice01", Practice01()));
    data.add(ItemBean("Practice02", Practice02()));
    data.add(ItemBean("Practice03", Practice03()));
    data.add(ItemBean("Practice04", Practice04()));
    data.add(ItemBean("Practice05", Practice05()));
    data.add(ItemBean("Practice06", Practice06()));
    data.add(ItemBean("FormLoginTest", FormLoginTest()));
    data.add(ItemBean("RouteAppDemo", RouteAppDemo()));
    data.add(ItemBean("BottomNavigationDemo", BottomNavigationDemo()));
    data.add(ItemBean("DrawerDemo", DrawerDemo()));
    data.add(ItemBean("TextFieldDemo", TextFieldDemo()));
    data.add(ItemBean("CupertinoStyleDemo", CupertinoStyleDemo()));
    data.add(ItemBean("CupertinoNavigatorDemo", CupertinoNavigatorDemo()));
    data.add(ItemBean("PageLayoutDemo", PageLayoutDemo()));
    data.add(ItemBean("PageLayoutDemo2", PageLayoutDemo2()));
    data.add(ItemBean("PageLayoutDemo3", PageLayoutDemo3()));
    data.add(ItemBean("RouteDemo", RouteDemo()));
    data.add(ItemBean("RouteDemo2", RouteDemo2()));
    data.add(ItemBean("OpacityDemo", OpacityDemo()));
    data.add(ItemBean("DecoratedBoxDemo", DecoratedBoxDemo()));
    data.add(ItemBean("RotatedBoxedDemo", RotatedBoxedDemo()));
    data.add(ItemBean("ClipDemo", ClipDemo()));
    data.add(ItemBean("CanvasDemo", CanvasDemo()));
    data.add(ItemBean("AnimationDemo", AnimationDemo()));
    data.add(ItemBean("微信UI练习", WeixinMain()));
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        primarySwatch: Colors.blue,
      ),
      home: new MainListWidget(data),
    );
  }
}

//首页
class MainListWidget extends StatelessWidget {
  List<ItemBean> data = [];

  MainListWidget(this.data);

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Flutter学习集合'),
      ),
      body:
          new ListView.builder(itemBuilder: (BuildContext context, int index) {
        if (index.isOdd) {
          return new Divider();
        }
        int i = index ~/ 2;
        if (i < data.length) {
          return buildRow(context, i);
        }
      }),
    );
  }

  Widget buildRow(BuildContext context, int index) {
    return new ListTile(
      title: Text(
        data[index].title,
        style: TextStyle(fontSize: 18.0),
      ),
      onTap: () {
        Navigator.of(context)
            .push(MaterialPageRoute(builder: (BuildContext context) {
          return data[index].page;
        }));
      },
    );
  }
}

//            ------------------分割线-----------------------                 //

class RandomWords extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new RandomWordsState();
  }
}

class RandomWordsState extends State<RandomWords> {
  final List<WordPair> _suggestions = <WordPair>[];
  final Set<WordPair> _saved = new Set<WordPair>();
  final TextStyle _biggerFont = const TextStyle(fontSize: 18.0);

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Startup Name Generator'),
        actions: <Widget>[
          new IconButton(
              icon: const Icon(Icons.list), onPressed: _pushSavedPage),
        ],
      ),
      body: _buildSuggestions(),
    );
  }

  //打开页面
  void _pushSavedPage() {
    Navigator.of(context)
        .push(new MaterialPageRoute(builder: (BuildContext context) {
      final Iterable<ListTile> tiles = _saved.map((WordPair pair) {
        return new ListTile(
          title: new Text(
            pair.asPascalCase,
            style: _biggerFont,
          ),
        );
      });
      final List<Widget> divided =
          ListTile.divideTiles(context: context, tiles: tiles).toList();

      return new Scaffold(
        appBar: new AppBar(
          title: const Text('saved suggestions'),
        ),
        body: new ListView(
          children: divided,
        ),
      );
    }));
  }

  //构建列表UI
  Widget _buildSuggestions() {
    return new ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        if (index.isOdd) {
          return new Divider();
        }

        final int i = index ~/ 2;
        if (i >= _suggestions.length) {
          _suggestions.addAll(generateWordPairs().take(10));
        }

        return _buildRow(_suggestions[i]);
      },
    );
  }

  // 构建列表条目UI
  Widget _buildRow(WordPair wordPair) {
    final bool alreadySaved = _saved.contains(wordPair);
    return new ListTile(
      title: new Text(
        wordPair.asPascalCase,
        style: _biggerFont,
      ),
      trailing: new Icon(
        alreadySaved ? Icons.favorite : Icons.favorite_border,
        color: alreadySaved ? Colors.red : null,
      ),
      onTap: () {
        setState(() {
          if (alreadySaved) {
            _saved.remove(wordPair);
          } else {
            _saved.add(wordPair);
          }
        });
      },
    );
  }
}
